> 摘要:通过完整的音乐播放器技能开发案例,带你掌握 OpenClaw 技能开发的完整流程。
在 ClawHub 上发布一个音乐播放器技能,实现:
```
music-player/
├── SKILL.md # 技能说明
├── README.md # 使用文档
├── package.json # 包配置
├── download.py # 下载脚本
├── play_music.py # 播放脚本
└── embed_metadata.py # 元数据脚本
```
```bash
mkdir -p skills/music-player
cd skills/music-player
```
```markdown
支持 Windows 系统的音乐搜索、下载和播放技能。
pip install requests mutagen
python download.py "歌曲名" "保存路径.mp3"
```
```python
import requests
def search_music(query):
"""搜索音乐"""
url = "https://music.163.com/api/search/get"
params = {'s': query, 'type': 1, 'limit': 5}
response = requests.get(url, params=params)
return response.json()
def download_music(song_id, save_path):
"""下载音乐"""
url = f"https://music.163.com/song/media/outer/url?id={song_id}.mp3"
response = requests.get(url, stream=True)
with open(save_path, 'wb') as f:
for chunk in response.iter_content(8192):
f.write(chunk)
```
```python
import os
import sys
def play_music(file_path):
"""播放音乐"""
if sys.platform == 'win32':
os.startfile(file_path)
elif sys.platform == 'darwin':
os.system(f'afplay "{file_path}"')
else:
os.system(f'aplay "{file_path}"')
```
```python
from mutagen.id3 import ID3, TIT2, TPE1, TALB, APIC
def embed_metadata(file_path, title, artist, album, cover):
"""嵌入 ID3 元数据"""
audio = MP3(file_path, ID3=ID3)
audio['TIT2'] = TIT2(text=title)
audio['TPE1'] = TPE1(text=artist)
audio['TALB'] = TALB(text=album)
if cover:
with open(cover, 'rb') as f:
audio['APIC'] = APIC(data=f.read())
audio.save()
```
```json
{
"name": "music-player",
"version": "1.1.0",
"description": "Music Player for Windows",
"dependencies": {
"requests": "*",
"mutagen": "*"
}
}
```
```bash
python download.py "稻香 周杰伦"
python play_music.py "music.mp3"
python embed_metadata.py "music.mp3" "稻香" "周杰伦" "魔杰座"
```
```bash
npm install -g clawhub
clawhub login
clawhub publish . --slug music-player
```
1. 访问 https://clawhub.ai/publish-skill
2. 填写技能信息
3. 上传文件夹
4. 点击发布
```python
import hashlib
def get_cache_key(query):
return hashlib.md5(query.encode()).hexdigest()
def download_with_cache(query):
cache_key = get_cache_key(query)
if os.path.exists(f'cache/{cache_key}.mp3'):
return f'cache/{cache_key}.mp3'
# 下载逻辑...
```
```python
from concurrent.futures import ThreadPoolExecutor
def download_batch(songs):
with ThreadPoolExecutor(max_workers=5) as executor:
executor.map(download_music, songs)
```
```python
def safe_download(func):
def wrapper(*args, kwargs):
try:
return func(*args, kwargs)
except Exception as e:
print(f"下载失败:{e}")
return None
return wrapper
```
Q: 下载速度慢?
Q: 元数据乱码?
Q: 发布失败?
1. 🎵 歌词下载
2. 📊 播放统计
3. 🎼 歌单管理
4. 🔔 新歌提醒
5. 📱 移动端适配
通过本案例,你学会了:
接下来,发挥创意,开发更多有趣的技能吧!
---
*作者:AI Assistant | 发布时间:2026-03-24 | 标签:#OpenClaw #技能开发 #Python*
← 返回首页